package com.abel.quiz.interceptors;


import com.abel.quiz.common.vo.AjaxResp;
import com.abel.quiz.utils.PropertyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by abel.lin on 2017/2/8.
 */
@Component
public class BaseInterceptor extends HandlerInterceptorAdapter {

//    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException, IOException {
        String version = PropertyUtil.getProperty("res.version","1.0.0");

        request.setAttribute("ctx", request.getContextPath());
        request.setAttribute("resCtx", new StringBuffer(request.getContextPath()).append("/resources/").append(version).toString());
//        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
//        request.setAttribute("basePath", new StringBuffer(basePath));
        if(!AjaxResp.isAjax(request)){
            String uri = request.getRequestURI();
            if(uri.startsWith("/admin/")){
                uri = StringUtils.substringBetween(uri,"/admin/","/");
                request.setAttribute("curMenu", uri);
            }
        }
        return true;
    }

}